package fr.greta.centreFormation.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.greta.centreFormation.bean.Cursus;
import fr.greta.centreFormation.bean.Module;
import fr.greta.centreFormation.bean.UniteEnseignement;
import fr.greta.centreFormation.dao.Bdd;
import fr.greta.centreFormation.dao.CursusHome;
import fr.greta.centreFormation.dao.ModuleHome;
import fr.greta.centreFormation.dao.UniteEnseignementHome;

/**
 * Servlet implementation class ScriptModifUE
 */
public class ScriptModifUE extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ScriptModifUE() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		////----------------------------------\\\\
		///		V�rification des donn�es	   \\\
		//--------------------------------------\\

		String intitule = request.getParameter("intitule");
		String description = request.getParameter("description");
		String[] lesModules = request.getParameterValues("lesModules");


		System.out.println(lesModules);

		// Si tableau de unite existe alors convertion

		Hashtable<Integer, String> lesMmoduleDuCursus = new Hashtable<Integer, String>();
		if(lesModules != null){
			for(String mo : lesModules){
				Integer idMo = Integer.parseInt(mo);
				Module m = ModuleHome.selectUnModuleById(idMo);
				lesMmoduleDuCursus.put(m.getIdModule(), m.getIntituleModule());
			}
		}

		HttpSession session = request.getSession(true);

		if(intitule == "" || description == "" || lesModules == null){

			System.out.println("manque donner");

			UniteEnseignement laUE = new UniteEnseignement();
			laUE.setIntituleUniteEnseignement(intitule);
			laUE.setDescriptionUniteEnseignement(description);

			session.setAttribute("laUE", laUE);
			session.setAttribute("lesModule", lesMmoduleDuCursus);

			session.setAttribute("etat", "Certain champs n'ont pas �t� remplis.");

			request.getRequestDispatcher("formulaireUE.jsp").forward(request, response);

		}else{

			////----------------------------------\\\\
			///		Insertion dans la base		   \\\
			//--------------------------------------\\

			Bdd connection =  new Bdd();
			UniteEnseignement ue = new UniteEnseignement();

			//R�cup�ration des Modules =>>
			ArrayList<Module> collectionDeModules = new ArrayList<Module>();

			for(String i : lesModules){
				Integer y = Integer.parseInt(i);
				Module m = ModuleHome.selectUnModuleById(y);
				collectionDeModules.add(m);
			}

			ue.setIdUniteEnseignement((Integer) session.getAttribute("idUe"));
			ue.setDescriptionUniteEnseignement(description);
			ue.setIntituleUniteEnseignement(intitule);
			ue.setCollectionDeModules(collectionDeModules);

			System.out.println("resultat update => "+UniteEnseignementHome.updateUneUniteEnseignement(ue));

			response.sendRedirect("ModifUE");
			//request.getRequestDispatcher("ModifUE").forward(request, response);


		}


	}

}
